package services

import (
	"github.com/mlogclub/simple/sqls"
	"github.com/mlogclub/simple/web/params"

	"bbs-go/internal/cache"
	"bbs-go/internal/models"
	"bbs-go/internal/repositories"
)

var UserDepartmentService = newUserDepartmentService()

func newUserDepartmentService() *userDepartmentService {
	return &userDepartmentService{}
}

type userDepartmentService struct {
}

func (s *userDepartmentService) Get(id int64) *models.UserDepartment {
	return repositories.UserDepartmentRepository.Get(sqls.DB(), id)
}

func (s *userDepartmentService) Take(where ...interface{}) *models.UserDepartment {
	return repositories.UserDepartmentRepository.Take(sqls.DB(), where...)
}

func (s *userDepartmentService) Find(cnd *sqls.Cnd) []models.UserDepartment {
	return repositories.UserDepartmentRepository.Find(sqls.DB(), cnd)
}

func (s *userDepartmentService) FindPageByParams(params *params.QueryParams) (list []models.UserDepartment, paging *sqls.Paging) {
	return repositories.UserDepartmentRepository.FindPageByParams(sqls.DB(), params)
}

func (s *userDepartmentService) FindPageByCnd(cnd *sqls.Cnd) (list []models.UserDepartment, paging *sqls.Paging) {
	return repositories.UserDepartmentRepository.FindPageByCnd(sqls.DB(), cnd)
}

func (s *userDepartmentService) Create(t *models.UserDepartment) error {
	err := repositories.UserDepartmentRepository.Create(sqls.DB(), t)
	cache.UserDepartmentCache.Invalidate(t.Id)
	return err
}

func (s *userDepartmentService) Update(t *models.UserDepartment) error {
	err := repositories.UserDepartmentRepository.Update(sqls.DB(), t)
	cache.UserDepartmentCache.Invalidate(t.Id)
	return err
}

func (s *userDepartmentService) Updates(id int64, columns map[string]interface{}) error {
	err := repositories.UserDepartmentRepository.Updates(sqls.DB(), id, columns)
	cache.UserDepartmentCache.Invalidate(id)
	return err
}

func (s *userDepartmentService) UpdateColumn(id int64, name string, value interface{}) error {
	err := repositories.UserDepartmentRepository.UpdateColumn(sqls.DB(), id, name, value)
	cache.UserDepartmentCache.Invalidate(id)
	return err
}

func (s *userDepartmentService) FindDepartmentList(adminId int) (list []models.UserDepartment, err error) {
	return repositories.UserDepartmentRepository.FindDepartmentList(sqls.DB(), adminId)
}

func (s *userDepartmentService) FindDepartmentSimpleList() (list []models.UserDepartmentSimpleResponse, err error) {
	return repositories.UserDepartmentRepository.FindDepartmentSimpleList(sqls.DB())
}
